package com.xinruke.hostels.central.manage.service;

import com.xinruke.hostels.central.manage.interfaces.dto.*;
import com.xinruke.hostels.central.manage.mapper.XtZuzhijigouMapper;
import com.xinruke.hostels.common.po.XtCaidan;
import com.xinruke.hostels.common.po.XtZuzhijigou;
import com.xinruke.hostels.common.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GuoZheng
 * @ClassName OrganizationService
 * @date：2019/6/24
 * @version: V1.0.0
 * @description：
 */
@Service
public class OrganizationService {

    @Resource
    private XtZuzhijigouMapper xtZuzhijigouMapper;


    public ResultVO add(OrganizationAddDTO organizationAddDTO) {
        ResultVO<List<OrganizationQueryResultDTO>> resultVO = ResultVO.fail();

        XtZuzhijigou xtZuzhijigou = new XtZuzhijigou();
        BeanUtils.copyProperties(organizationAddDTO, xtZuzhijigou);
        xtZuzhijigou.setChuangjianshijian(new Date());
        xtZuzhijigou.setChuangjianrenId(0);//todo 当前用户
        xtZuzhijigou.setShanchubiaoshi(XtZuzhijigou.SHANCHUBIAOSHI_WU_XIOA);
        xtZuzhijigou.setGengxinshijian(new Date());
        xtZuzhijigou.setGengxinrenId(0);

        int insert = xtZuzhijigouMapper.insert(xtZuzhijigou);
        if (insert > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;

    }

    public ResultVO delete(OrganizationDeleteDTO organizationDeleteDTO) {

        ResultVO<List<OrganizationQueryResultDTO>> resultVO = ResultVO.fail();

        XtZuzhijigou xtZuzhijigou = new XtZuzhijigou();
        xtZuzhijigou.setId(organizationDeleteDTO.getId());
        xtZuzhijigou.setGengxinrenId(0);
        xtZuzhijigou.setGengxinshijian(new Date());
        xtZuzhijigou.setShanchubiaoshi(XtZuzhijigou.SHANCHUBIAOSHI_YOU_XIAO);

        int insert = xtZuzhijigouMapper.updateByPrimaryKeySelective(xtZuzhijigou);
        if (insert > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }


    public ResultVO modifyOrganization(OrganizationModifyDTO organizationModifyDTO) {

        ResultVO<List<OrganizationQueryResultDTO>> resultVO = ResultVO.fail();

        XtZuzhijigou xtZuzhijigou = new XtZuzhijigou();
        BeanUtils.copyProperties(organizationModifyDTO,xtZuzhijigou);

        xtZuzhijigou.setGengxinrenId(0);
        xtZuzhijigou.setGengxinshijian(new Date());


        int insert = xtZuzhijigouMapper.updateByPrimaryKeySelective(xtZuzhijigou);
        if (insert > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }


    public ResultVO<List<OrganizationQueryResultDTO>> getorganizationTree(OrganizationQueryDTO organizationQueryDTO) {
        ResultVO<List<OrganizationQueryResultDTO>> resultVO = ResultVO.fail();
        List<XtZuzhijigou> allOrgList = xtZuzhijigouMapper.selectByExample(Example.builder(XtZuzhijigou.class)
                .where(Sqls.custom().andEqualTo("shanchubiaoshi", XtCaidan.SHANCHUBIAOSHI_WU_XIOA)).build());

        List<XtZuzhijigou> subOrgList = allOrgList.stream().filter(o -> o.getFujigouId() == organizationQueryDTO.getRootId()).collect(Collectors.toList());

        //递归查询下级
        List<OrganizationQueryResultDTO> dataVos = getSubOrgList(subOrgList, allOrgList);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(dataVos);
        return resultVO;
    }

    //递归查询下级
    private List<OrganizationQueryResultDTO> getSubOrgList(List<XtZuzhijigou> parentOrgList, List<XtZuzhijigou> allOrgList) {


        return parentOrgList.stream().map(p -> {
            OrganizationQueryResultDTO parentDto = new OrganizationQueryResultDTO();
            BeanUtils.copyProperties(p, parentDto);

            List<XtZuzhijigou> subList = allOrgList.stream().filter(o -> o.getFujigouId() == parentDto.getId()).sorted(Comparator.comparing(XtZuzhijigou::getPailieshunxu)).collect(Collectors.toList());
            parentDto.getSubOrgDtoList().addAll(getSubOrgList(subList, allOrgList));

            return parentDto;
        }).collect(Collectors.toList());
    }


    public ResultVO<OrganizationDetailQueryResultDTO> detail(OrganizationDetailQueryDTO organizationQueryDTO) {
        ResultVO<OrganizationDetailQueryResultDTO> resultVO = ResultVO.fail();
        OrganizationDetailQueryResultDTO dataDto = new OrganizationDetailQueryResultDTO();
        XtZuzhijigou xtZuzhijigou = xtZuzhijigouMapper.selectByPrimaryKey(organizationQueryDTO.getId());
        XtZuzhijigou fujigou = xtZuzhijigouMapper.selectByPrimaryKey(xtZuzhijigou.getFujigouId());
        if(fujigou == null){
            resultVO.setMessage("该机构上级不存在");
            return resultVO;
        }
        BeanUtils.copyProperties(xtZuzhijigou,dataDto);
        //todo 数据
        dataDto.setFujigouMingcheng(fujigou.getMingcheng());
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(dataDto);
        return resultVO;
    }
}
